<?php
/**
 * 生成纯农历格式
 */
class _interface extends __interface {
	
	/**
	 * (non-PHPdoc) @see __interface::dispose_data()
	 */
	public function dispose_data() {
		global $cache;
		
		if($cache->exists("weather:lunar:". $this->cityid)){
			$s = $cache->fetch("weather:lunar:". $this->cityid);
			$s = str_replace("{time}", date("Y-m-d H:i:s"), $s);
			header ( "Content-Type: text/calendar; charset=utf-8" );
			echo $s;
			exit;
		}
		
		// 获取要显示开开始和结束日期
		$this->day_begin = dateadd ( "day", - $this->style ["festival_before_days"] );
		$this->day_end = dateadd ( "day", $this->style ["festival_later_days"] );
		
		$s = "BEGIN:VCALENDAR\r\n";
		$s .= "PRODID:-//mdeve.com//农历、黄历、节日//CN\r\n";
		$s .= "VERSION:2.0\r\n";
		$s .= "METHOD:PUBLISH\r\n";
		$s .= "X-CALSTART:" . date ( "Ymd", $this->day_begin ) . "T000000\r\n";
		$s .= "X-CALEND:" . date ( "Ymd", $this->day_end ) . "T000000\r\n";
		$s .= "X-WR-CALNAME:农历、黄历、节日\r\n";
		$s .= "X-APPLE-CALENDAR-COLOR:#993333\r\n";
		$s .= "X-WR-TIMEZONE:Asia/Shanghai\r\n";
		
		// /////////////////////////////////////////////////////////////////////////////////////////////////////
		for($i = - $this->style ["festival_before_days"]; $i <= $this->style ["festival_later_days"]; $i ++) {
			$date = date ( "Y-m-d", dateadd ( "day", $i ) );
			$date = strtotime ( $date );
			// 如果要显示农历
			$festival_data = array ();
			// 如果要显示农历
			
			$festival = init_festival ();
			$festival_data = $festival->get ( $date );
			
			$day_info = array ();
			$day_info [] = $festival_data ["lunar"];
			// 获取固定节日
			if (isset ( $festival_data ["festival_fixed"] )) {
				foreach ( $festival_data ["festival_fixed"] as $r ) {
					$day_info [] = $r;
				}
			}
			// 获取不固定节日
			if (isset ( $festival_data ["festival_unstable"] )) {
				foreach ( $festival_data ["festival_unstable"] as $r ) {
					$day_info [] = $r;
				}
			}
			// 获取农历节气
			if (isset ( $festival_data ["solar_festival"] )) {
				foreach ( $festival_data ["solar_festival"] as $r ) {
					$day_info [] = $r;
				}
			}
			// 获取农历节日
			if (isset ( $festival_data ["lunar_festival"] )) {
				foreach ( $festival_data ["lunar_festival"] as $r ) {
					$day_info [] = $r;
				}
			}
			// 获取数据完毕，开始生成ics文件
			$info = "---------- 今日详情 ----------\\n" . $day_info [0] . "\\n";
			$detail = "";
			
			if (sizeof ( $day_info ) > 1) {
				for($x = 1; $x < sizeof ( $day_info ); $x ++) {
					$info .= $day_info [$x] ["title"];
					if (isset ( $day_info [$x] ["beginyear"] )) {
						$num = date ( "Y" ) - $day_info [$x] ["beginyear"] + 1;
						$info .= " （第{$num}个）";
					}
					$info .= "\\n";
					
					$detail .= "\\n---------- " . $day_info [$x] ["title"] . " ----------\\n";
					$detail .= $day_info [$x] ["content"] . "\\n";
				}
			}
			
			// 获取黄历
			if (isset ( $festival_data ["almanac"] )) {
				$detail .= "\\n---------- 今日黄历 ----------\\n";
				foreach ( $festival_data ["almanac"] as $almanac ) {
					$detail .= "{$almanac}\\n";
				}
			}
			
			if ($i == 0) { // 如果是今天，开始时间则为当前
				$detail .= "\\n\\n更新时间：{time}\\n";
			}
			$s .= "BEGIN:VEVENT\r\n";
			$s .= "CLASS:PUBLIC\r\n";
			$s .= "CREATED:" . date ( "Ymd" ) . "T" . date ( "His" ) . "Z\r\n";
			$s .= "DESCRIPTION:{$info}\\n{$detail}\r\n";
			$s .= "DTSTAMP:" . date ( "Ymd", $date ) . "T" . date ( "His" ) . "Z\r\n";
			if ($i == 0) { // 如果是今天，开始时间则为当前
				$s .= "DTSTART;TZID=Asia/Chongqing:" . date ( "Ymd", $date ) . "T" . date ( "Hi00" ) . "\r\n";
				$s .= "DTEND;TZID=Asia/Chongqing:" . date ( "Ymd", $date ) . "T235959" . "\r\n";
			} else {
				//$s .= "DTSTART;VALUE=DATE:" . date ( "Ymd", $date ) . "\r\n";
				//$s .= "DTEND;VALUE=DATE:" . date ( "Ymd", dateadd ( 'day', 1, $date ) ) . "\r\n";
				$s .= "DTSTART;TZID=Asia/Chongqing:" . date ( "Ymd", $date ) . "T000000" . "\r\n";
				$s .= "DTEND;TZID=Asia/Chongqing:" . date ( "Ymd", $date ) . "T235959" . "\r\n";
			}
			$s .= "LAST-MODIFIED:" . date ( "Ymd" ) . "T" . date ( "His" ) . "Z\r\n";
			$s .= "SEQUENCE:0\r\n";
			$s .= "PRIORITY:6\r\n";
			$s .= "SUMMARY:" . $day_info [0] . "\r\n";
			$s .= "TRANSP:TRANSPARENT\r\n";
			$s .= "UID:" . $this->create_uid ($date, "n1") . "\r\n";
			$s .= "STATUS:TENTATIVE\r\n";
			$s .= "CATEGORIES:FREE\r\n";
			$s .= "END:VEVENT\r\n";
		}
		
		// /////////////////////////////////////////////////////////////////////////////////////////////////////
		
		$s .= "END:VCALENDAR";
		$cache->store("weather:lunar:". $this->cityid, $s, 1800);		// 缓存半小时
		$s = str_replace("{time}", date("Y-m-d H:i:s"), $s);
		header ( "Content-Type: text/calendar; charset=utf-8" );
		echo $s;
	}
	
	
	
    private function create_uid($date, $flag="weather") {
		//return strtoupper ( $this->randomstr ( 8 ) . "-" . $this->randomstr ( 4 ) . "-" . $this->randomstr ( 4 ) . "-" . $this->randomstr ( 4 ) . "-" . $this->randomstr ( 12 ) );
		if(strtotime($date)>0) $date = strtotime($date);
		return "{$flag}_".date("Ymd", $date). "@mdeve.com";
	}
	
	private function randomstr($length, $numeric = 0) {
		mt_srand ( ( double ) microtime () * 1000000 );
		if ($numeric) {
			$hash = sprintf ( '%0' . $length . 'd', mt_rand ( 0, pow ( 10, $length ) - 1 ) );
		} else {
			$hash = '';
			$chars = 'ABCDEF0123456789';
			$max = strlen ( $chars ) - 1;
			for($i = 0; $i < $length; $i ++) {
				$hash .= $chars [mt_rand ( 0, $max )];
			}
		}
		return $hash;
	}
}